package com.example.myapplication8;
// MainViewModel.java
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.LiveData;
import java.util.ArrayList;
import java.util.List;

public class MainViewModel extends ViewModel {
    private final MutableLiveData<List<DataModel>> dataList = new MutableLiveData<>();

    public MainViewModel() {
        List<DataModel> initialData = new ArrayList<>();
        initialData.add(new DataModel(1, "个人信息1", "这是个人信息1的详细内容"));
        initialData.add(new DataModel(2, "个人信息2", "这是个人信息2的详细内容"));
        initialData.add(new DataModel(3, "个人信息3", "这是个人信息3的详细内容"));
        dataList.setValue(initialData);
    }

    public LiveData<List<DataModel>> getDataList() {
        return dataList;
    }

    public void refreshData() {
        List<DataModel> newData = new ArrayList<>();
        newData.add(new DataModel(1, "个人信息1（刷新后）", "这是个人信息1刷新后的详细内容"));
        newData.add(new DataModel(2, "个人信息2（刷新后）", "这是个人信息2刷新后的详细内容"));
        newData.add(new DataModel(3, "个人信息3（刷新后）", "这是个人信息3刷新后的详细内容"));
        dataList.setValue(newData);
    }

    public void deleteItem(int id) {
        List<DataModel> currentList = dataList.getValue();
        if (currentList != null) {
            List<DataModel> newList = new ArrayList<>(currentList);
            newList.removeIf(item -> item.getId() == id);
            dataList.setValue(newList);
        }
    }
}